<?php
	/*
	 *供应商管理控制器类
	 */
namespace Admin\Controller;
use Think\Controller;
class SupperController extends AdminController {
	//供应商列表
	public function index()
	{
		$yid=!empty($_SESSION['yid'])?$_SESSION['yid']:1;
		$page=I('get.page','1','strip_tags');
        $count=M('supper_user')->where('isdel=%d AND yid=%d',0,$yid)->count();
        $pagearr= getPageArr($page,C('PAGESIZE'),$count);
		$result=M('supper_user')->where('isdel=%d AND yid=%d',0,$yid)
		                 ->limit($pagearr['start'],$pagearr['pagesize'])
		                 ->order('supperid desc')->select();

		$this->assign('suppers',$result);
		$this->assign('pagearr',$pagearr);
		$this->display('index');
	}
	/*
	 *编辑供应商
	 *@param supperid 
	 *@param yid 运营商id
	 */
	public function editSupper(){
		if(IS_POST)
		{
		$data['name']=I('post.name','','strip_tags');
		$data['contactname']=I('post.contactname','','strip_tags');
		$data['secondcontact']=I('post.secondcontact','','strip_tags');
		$data['companyname']=I('post.companyname','','strip_tags');
		$data['mobile']=I('post.mobile','','strip_tags');
		$data['telephone']=I('post.telephone','','strip_tags');
		$data['email']=I('post.email','','strip_tags');
		$data['qq']=I('post.qq','','strip_tags');
		$data['address']=I('post.address','','strip_tags');
		$pass=I('post.pass','','strip_tags');
		$data['regtime']=time();
		$data['yid']=$_SESSION['yid'];
		$where['supperid']=$_GET['supperid'];
		if (!empty($pass))
			{
				
				$data['pass']=md5($pass);

			}
		
   
		 $re=M('supper_user')->where($where)->save($data);
			 
			  if($re!==false){
			 
			  	echo "<script>alert('供应商修改成功');location.href='/index.php/Admin/Supper/index'</script>";
			  }

		}else
		{
		$where['supperid']=$_GET['supperid'];
		$re=M('supper_user')->where($where)->getField("supperid,pass,name,email,mobile,telephone,address,secondcontact,contactname,qq,companyname");
		
		$this->assign('re',$re);
	  $this->display();
	}


	}

	/*
	 *添加供货商
	 */
	public function addSupper(){
	
		$data['name']=I('post.name','','strip_tags');
		$data['contactname']=I('post.contactname','','strip_tags');
		$data['secondcontact']=I('post.secondcontact','','strip_tags');
		$data['companyname']=I('post.companyname','','strip_tags');
		$data['mobile']=I('post.mobile','','strip_tags');
		$data['telephone']=I('post.telephone','','strip_tags');
		$data['email']=I('post.email','','strip_tags');
		$data['qq']=I('post.qq','','strip_tags');
		$data['address']=I('post.address','','strip_tags');
		$data['pass']=md5(I('post.pass','','strip_tags'));
		 $data['regtime']=time();
		 $data['yid']=$_SESSION['yid'];
		 $re=M('supper_user')->add($data);
		 if($re>0)
		 {
		 	echo "<script>alert('供应商添加成功');location.href='/index.php/Admin/Supper/index'</script>";
		 }else
		 {
		 	echo "<script>alert('供应商添加失败');location.href='/index.php/Admin/Supper/add'</script>";

		 }
	

		


		

	}


	/**
	 * 添加供应商静态显示
	 */
	public function add(){
		$this->display("addSupper");
	}
	public function saveSupper(){

	}
	/*
	 *保存更改后的供货商
	 *@yid 运营商id
	 *@param supperid 供货商id
	 */

	/**
	 * 检测用户名的唯一性
	 */
   public function weiyi()
	{
		$supper=M('supper_user');
		$action=I('post.action','','strip_tags');
		switch ($action) {
			case 'editsup':
				$supperid=I('post.supperid',0,'strip_tags');
				$where['companyname']=I('post.companyname','','strip_tags');
				$where['supperid']=array('neq',$supperid);
				$map['name']=I('post.name','','strip_tags');
				$map['supperid']=array('neq',$supperid);
			
				$result1=$supper->where($where)->count();

				$result2=$supper->where($map)->count();

				break;
			case 'addsup':
				$where['companyname']=I('post.companyname','','strip_tags');
				$map['name']=I('post.name','','strip_tags');
				$result1=$supper->where($where)->count();
				$result2=$supper->where($map)->count();
				break;
			
		}
		if($result1 > 0 || $result2 > 0)
		{
					$res['status']=0;
					$res['msg']='登录用户名或公司名称被占用';
					echo json_encode($res,JSON_UNESCAPED_UNICODE);
					return '';
		}
		$res['status']=1;
		echo json_encode($res,JSON_UNESCAPED_UNICODE);

	}
	
}